import WxRequest from '../plugins/wx-request/lib/index';
import { baseUrl } from '../utils/config.js';
const interceptors = {
  // 请求数据
  request(request) {
    const app = getApp();
    request.header['content-type'] = 'application/json;charset=utf-8';
    request.header.authorization = app.globalData.token;
    return request
  },
  // 请求失败
  requestError(requestError) {
    return Promise.reject(requestError)
  },
  // 响应数据
  response(response) {
    return response
  },
  // 响应失败
  responseError(responseError) {
    return Promise.reject(responseError)
  },
}
class BaseService extends WxRequest {
  constructor() {
    super({
      baseURL: baseUrl,
    })
    this.interceptors.use(interceptors);
  }
  get(url, config, showLoading){
    const _ = this;
    if (showLoading == undefined || showLoading == true){
      wx.showLoading({ title: '请稍后' })
    }
    return new Promise(function (resolve, reject){
      _.getRequest(url, config).then(response => {
        if (showLoading == undefined || showLoading == true) {
          wx.hideLoading()
        }
        if (response.statusCode == 200) {
          resolve(response.data);
        } else {
          reject(response);
        }
      }).catch(err => {
        if (showLoading == undefined || showLoading == true) {
          wx.hideLoading()
        }
        reject(err);
      });
    })
  }
  post(url, config, showLoading) {
    const _ = this;
    if (showLoading == undefined || showLoading == true) {
      wx.showLoading({ title: '请稍后' })
    }
    return new Promise(function (resolve, reject) {
      _.postRequest(url, config).then(response => {
        if (showLoading == undefined || showLoading == true) {
          wx.hideLoading()
        }
        if (response.statusCode == 200) {
          resolve(response.data);
        } else {
          reject(response);
        }
      }).catch(err => {
        if (showLoading == undefined || showLoading == true) {
          wx.hideLoading()
        }
        reject(err);
      });
    })
  }
  put(url, config, showLoading) {
    const _ = this;
    if (showLoading == undefined || showLoading == true) {
      wx.showLoading({ title: '请稍后' })
    }
    return new Promise(function (resolve, reject) {
      _.putRequest(url, config).then(response => {
        if (showLoading == undefined || showLoading == true) {
          wx.hideLoading()
        }
        if (response.statusCode == 200) {
          resolve(response.data);
        } else {
          reject(response);
        }
      }).catch(err => {
        if (showLoading == undefined || showLoading == true) {
          wx.hideLoading()
        }
        reject(err);
      });
    })
  }
  delete(url, config, showLoading) {
    const _ = this;
    if (showLoading == undefined || showLoading == true) {
      wx.showLoading({ title: '请稍后' })
    }
    return new Promise(function (resolve, reject) {
      _.deleteRequest(url, config).then(response => {
        if (showLoading == undefined || showLoading == true) {
          wx.hideLoading()
        }
        if (response.statusCode == 200) {
          resolve(response.data);
        } else {
          reject(response);
        }
      }).catch(err => {
        if (showLoading == undefined || showLoading == true) {
          wx.hideLoading()
        }
        reject(err);
      });
    })
  }
  
}
export default BaseService;

